package com.tip.model;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class ServerModel {
	
	public ServerModel(KVStore kvStore)
	{
		this.kvStore = kvStore;
		host = kvStore.getServerHost();
		port = kvStore.getServerPort();		
	}
	
	private static HttpMethod getMethod(String url, String param) throws IOException {
		GetMethod get = new GetMethod(url + "?" + param);
		get.releaseConnection();
		return get;
	}
	public List <TipData> listTip(String query)
	{
		try
		{
			String param = "query=" + URLEncoder.encode(query,"utf-8");
			HttpClient httpClient = new HttpClient();
			httpClient.getHostConfiguration().setHost(host,port, "http");
			HttpMethod method = getMethod(listUrl, param);
			httpClient.executeMethod(method);
			
			String response = method.getResponseBodyAsString();
//			InputStream inputStream = method.getResponseBodyAsStream();
			if (response!=null)
			{
				String SPLIT = "_-_";
				String sp[] = response.split(SPLIT);
				List <TipData> list = new ArrayList<TipData>();
				for (String tmp:sp)
				{
					if (tmp!=null && tmp.trim().length()>0)
					{
						int index = tmp.indexOf("~");
						String tmpData = tmp;
						String tmpName = null;
						String tag = "";
						if (index!=-1)
						{
							tag = tmp.substring(0,index);
							tmp = tmp.substring(index+1);
						}
						index = tmp.indexOf("~");
						if (index!=-1)
						{
							tmpName = tmp.substring(0,index);
							tmpData = tmp.substring(index+1);
						}
						
						list.add(new TipData(TipData.SERVER,tag,tmpName,tmpData));
					}
				}
				return list;
			}
			System.out.println(response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean saveTipData(TipData data) {
		HttpClient httpClient = new HttpClient();
		httpClient.getHostConfiguration().setHost(host,port,"http");
		PostMethod post = new PostMethod(saveUrl);
		post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
		NameValuePair[] param = { 
				new NameValuePair("name",data.getName()),
				new NameValuePair("tip", data.getData()),
				};
		post.setRequestBody(param);
		post.releaseConnection();
		try {
			httpClient.executeMethod(post);
			String response = new String(post.getResponseBodyAsString().getBytes("ISO-8859-1"));
			System.out.println(response);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	private String saveUrl = "/tip/admin/insert.htm";
	private String listUrl = "/tip/admin/searchTip.htm";
	//private String host = "10.20.157.162";
	private String host = "localhost";
	private int	   port = 8080;
	private KVStore kvStore;
	
}
